/*
 * drv_ht1621.c
 *
 *  Created on: Jan 3, 2025
 *      Author: buquan_chen
 */

/* Includes ------------------------------------------------------------------*/
#include "drv_ht1621.h"
#include <stdlib.h>

struct _HT1621_type *pt_HT1621 = NULL;

/**
 * @brief       : Send Data to HT1621
 * @param[in]   : Data - 8 bits data to be sent
 * @param[in]   : num - number of bits to be sent
 * @retval      : None
 */
void HT1621_Writebites(u8 Data,u8 num) 
{ 
  	u8 i; 
  	
  	for (i=0;i<num;i++)
 	{   
  		pt_HT1621->WR->reset();
        delay_us(3);
  		
    	if(((Data & 0x80)>>7)==1)
    	pt_HT1621->DATA->set();
    	if(((Data & 0x80)>>7)==0)
    	pt_HT1621->DATA->reset();
    	
    	pt_HT1621->WR->set();
        delay_us(3);
    	Data<<=1;
  	}                 
}

/**
 * @brief       : Write data to a specific address on the HT1621.
 * @param[in]   : Addr - The address to write the data to.
 * @param[in]   : Data - The data to be written.
 * @retval      : None
 */

void HT1621_WriteData(u8 Addr,u8 Data)
{
    if (pt_HT1621 == NULL)
    {
        return;
    }
    pt_HT1621->CS->reset();
    delay_us(10);
  	HT1621_Writebites(WrData,3);
  	HT1621_Writebites(Addr<<2,6);
  	HT1621_Writebites(Data<<4,4);
  	pt_HT1621->CS->set();
}

/**
 * @brief       : Write a command to the HT1621.
 * @param[in]   : Cmd - The command to be written.
 * @retval      : None
 */

void HT1621_WriteCmd(u8 Cmd) 
{ 
    if (pt_HT1621 == NULL)
    {
        return;
    }
  	pt_HT1621->CS->reset();
    delay_ms(1);
  	HT1621_Writebites(0x80,4);
  	HT1621_Writebites(Cmd,8);
  	pt_HT1621->CS->set();
}
/**
 * @brief       : Control the backlight of the HT1621.
 * @param[in]   : State - 1 for on, 0 for off.
 * @retval      : None
 */
void HT1621_Backlight_control(uint8_t state) 
{ 
    if (pt_HT1621 == NULL)
    {
        return;
    }
    if (state == 1)
    {
        pt_HT1621->BK->set();
    }
    else
    {
        pt_HT1621->BK->reset();
    }
}
/**
 * @brief       : Initialize the HT1621.
 * @param[in]   : CS  - The chip select pin.
 * @param[in]   : WR  - The write pin.
 * @param[in]   : DATA- The data pin.
 * @param[in]   : BK  - The backlight pin, can be NULL if not used.
 * @retval      : 0 on success, else an error code.
 */
uint8_t HT1621_Init(struct gpio_type *CS,struct gpio_type *WR,struct gpio_type *DATA,struct gpio_type *BK)
{
    pt_HT1621 = (struct _HT1621_type *)malloc(sizeof(struct _HT1621_type));
    pt_HT1621->CS = CS;
    pt_HT1621->WR = WR;
    pt_HT1621->DATA = DATA;
    pt_HT1621->BK = BK;

    pt_HT1621->CS->init(GPIO_Mode_Out_PP);
    pt_HT1621->WR->init(GPIO_Mode_Out_PP);
    pt_HT1621->DATA->init(GPIO_Mode_Out_PP);

    if (pt_HT1621->BK != NULL)
    {
        pt_HT1621->BK->init(GPIO_Mode_Out_PP);
    }

    pt_HT1621->CS->set();
    pt_HT1621->WR->set();
    pt_HT1621->DATA->set();
    delay_ms(20);

    HT1621_WriteCmd(HT1621_BIAS);
    HT1621_WriteCmd(HT1621_RC256);
    HT1621_WriteCmd(HT1621_SYS_DIS);
    HT1621_WriteCmd(HT1621_WDT_DIS);
    HT1621_WriteCmd(HT1621_SYS_EN);
    HT1621_WriteCmd(HT1621_LCD_ON);

    delay_ms(20);

    return 0;
}


/**
 * @brief       : Check if the HT1621 is initialized.
 * @retval      : 0 if initialized, else -1.
 */
uint8_t HT1621_Check()
{
    if (pt_HT1621 == NULL)
    {
        return -1;
    }

    return 0;
}



// void main(void)
// {
//    if(HT1621_Init(&pa0,&pa1,&pa2,&pa3)) printf("HT1621 init err\r\n");

//    while(1)
//    {
//        HT1621_Backlight_control(1);
//        for (int i = 0; i < 16; i++)
//        {
//             HT1621_WriteData(i,0x0F);
//             delay_ms(10);
//        }
//        for (int i = 0; i < 16; i++)
//        {
//             HT1621_WriteData(i,0x00);
//             delay_ms(10);
//        }
//        HT1621_Backlight_control(0);
//        delay_ms(1000);
//    }
// }


